local filename = "getturntableinfo.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
local redisdao = require "redisdao"
local base = require "base"
require "pubenum"

local json = require "cjson"
json.encode_sparse_array(true,1,1)

local service = nil
local CMD = {
	service = nil
}

--
--转盘
--
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end
    msgbody = msgbody or {}

    local response = { }
    local continuous_days = 0
    local turntable_times = 0

    --获得登录记录
    local result, data = redisdao.query_data(".gmmqredis", "get", rid .. ":loginrecord")
    if result and data then
        loginrecord = json.decode(data)
        if loginrecord then
            local cur_time = timetool.get_time()
            local cur_day = os.date("%d",cur_time)
            local last_day = os.date("%d",loginrecord.last_logintime)
            local dur_time = cur_time - loginrecord.last_logintime
            local oneday_time = 48 * 3600
            if dur_time >= 0 and dur_time < oneday_time then  --相邻2天内
                if cur_day ~= last_day then
                    continuous_days = continuous_days + loginrecord.continuous_days
                else
                    continuous_days = loginrecord.continuous_days
                    turntable_times = turntable_times + loginrecord.turntable_times
                end
            end
        end
    end

    --获得连续登陆奖励列表
    result, data = redisdao.query_data(".gmmqredis", "get", "login_prize_list")
    local loginprize_list = ""
    if result and data then
        loginprize_list = data
    end  

    --获得转盘配置
    result, data = redisdao.query_data(".gmmqredis", "get", "turntable_config")
    local turntable_config = nil
    if result and data then
        turntable_config = json.decode(data)
    end
    turntable_config = turntable_config or {max_times = 4, cost_list = {}, free_times = 1}

    --获得转盘奖励列表
    result, data = redisdao.query_data(".gmmqredis", "get", "turntable_prize_list")
    local tableprize_list = ""
    if result and data then
        tableprize_list = data
    end

    response.tableprize_list = tableprize_list
    response.loginprize_list = loginprize_list
    response.continuous_logindays = continuous_days or 0
    response.had_turn_times = turntable_times or 0
    response.max_turn_times = turntable_config.max_times or 4
    response.cost_prop_list = json.encode(turntable_config.cost_list or {})
    response.free_times = turntable_config.free_times or 1
    
	return response
end

return CMD
